package com.bingo.dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.management.Query;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.junit.Test;

import com.bingo.jdbc.JdbcUtils;
import com.mchange.v2.c3p0.impl.NewPooledConnection;

public class Demo3 {
	@Test
	public void fun1() throws SQLException{
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "insert into t_stu values(?,?,?,?)";
		Object[] params = {1005,"johnny",88,"female"};
		
		qr.update(sql,params);
	}
	@Test
	public void fun2() throws SQLException{
		//创建QueryRunner，需要数据库连接池模板
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		//给出sql模板
		String sql = "select * from t_stu where sid=?";
		//给出参数
		Object[] params = {1005};
		/*ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() {

			@Override
			public Stu handle(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				return null;
			}
		};*/
		//执行query（）方法，需要给出结果集处理器，即ResultSetHandler实现类
		//本例给出的是BeanHandler，它实现了ResultSetHandler
		//它需要一个类型，然后他会把rs中的数据封装到指定类型的javabean对象中，然后返回javabean对象
		Stu stu = qr.query(sql,new BeanHandler<Stu>(Stu.class),params);
		System.out.println(stu);
		
	}
}
